package com.algorighm.hqy;

/**
 * @Classname DataNode
 * @Date 2019/9/19 16:08
 * @auther by hqy
 */
public class DataNode<T> {

    private T t;

    public DataNode() {

    }

    public DataNode(T t) {
        this.t = t;
    }

    private DataNode right;

    private DataNode left;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    public DataNode getRight() {
        return right;
    }

    public void setRight(DataNode right) {
        this.right = right;
    }

    public DataNode getLeft() {
        return left;
    }

    public void setLeft(DataNode left) {
        this.left = left;
    }


    public DataNode preOrder(T t) {
        DataNode find = null;
        if (this.t == t) {
            return this;
        }
        if (this.left != null) {
            find = this.left.preOrder(t);
        }
        if(find!=null){
            return find;
        }
        if (this.right != null) {
            find = this.right.preOrder(t);
        }
        return find;
    }

    public DataNode infixOrder(T t) {
        DataNode find = null;
        if (this.left != null) {
            find = this.left.preOrder(t);
        }
        if(find!=null){
            return find;
        }
        if (this.t == t) {
            return this;
        }
        if (this.right != null) {
            return this.right.preOrder(t);
        }
        return find;
    }

    public DataNode postOrder(T t) {
        DataNode find = null;
        if (this.left != null) {
            find = this.left.preOrder(t);
        }
        if(find!=null){
            return find;
        }
        if (this.right != null) {
            find = this.right.preOrder(t);
        }
        if (this.t == t) {
            return this;
        }
        return find;
    }


    /**
     * @param []
     * @return void
     * @Description 二叉树的前序遍历，先将当前节点输出，然后遍历完左子树，在遍历完右子数
     */
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * @param []
     * @return void
     * @Description 二叉树中序遍历，先遍历完左子树，输出，然后遍历右子树
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * @param []
     * @return void
     * @Description 后序遍历，先遍历完左子树，然后遍历完右子树，输出
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ",value:" + t;
    }
}
